﻿
using DryIoc;
using HotelManager.Common;
using HotelManager.Service;
using HotelManager.ViewModels;
using HotelManager.Views;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Services.Dialogs;
using System.Configuration;
using System.Data;
using System.Windows;

namespace HotelManager
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            return Container.Resolve<MainView>();
        }
        public static void LogOut(IContainerProvider container)
        {
            Current.MainWindow.Hide();
            var dialog = container.Resolve<IDialogService>();
            dialog.ShowDialog("LoginView", callback =>
            {
                if (callback.Result != ButtonResult.OK)
                {
                    Environment.Exit(0);
                    return;
                }
                Current.MainWindow.Show();
            });
        }
        protected override void OnInitialized()
        {
            var dialog = Container.Resolve<IDialogService>();
            dialog.ShowDialog("LoginView", callback =>
            {
                if(callback.Result!=ButtonResult.OK)
                {
                    Application.Current.Shutdown();
                    return;
                }
                var service = App.Current.MainWindow.DataContext as IConfigureService;
                if(service!=null)
                    service.Configure();
                base.OnInitialized();
            });
        }
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.GetContainer().Register<HttpRestClient>(made: Parameters.Of.Type<string>(serviceKey: "webUrl"));
            containerRegistry.GetContainer().RegisterInstance(@"http://localhost:5241/", serviceKey: "webUrl");
            containerRegistry.Register<IDialogHostService, DialogHostService>();
            containerRegistry.Register<ILoginService, LoginService>();
            containerRegistry.Register<IRoleService, RoleService>();
            containerRegistry.Register<IAdminService, AdminService>();
            containerRegistry.Register<ITypeService, TypeService>();
            containerRegistry.Register<IRoomStateService, RoomStateService>();
            containerRegistry.Register<IRoomService, RoomService>();
            containerRegistry.Register<IResideStateService, ResideStateService>();
            containerRegistry.Register<IGuestService, GuestService>();
            containerRegistry.RegisterDialog<LoginView, LoginViewModel>();
            containerRegistry.RegisterForNavigation<IndexView, IndexViewModel>();
            containerRegistry.RegisterForNavigation<MsgView, MsgViewModel>();
            containerRegistry.RegisterForNavigation<RoleView, RoleViewModel>();
            containerRegistry.RegisterForNavigation<AdminView, AdminViewModel>();
            containerRegistry.RegisterForNavigation<TypeView, TypeViewModel>();
            containerRegistry.RegisterForNavigation<RoomView, RoomViewModel>();
            containerRegistry.RegisterForNavigation<TotalView, TotalViewModel>();
            containerRegistry.RegisterForNavigation<GuestView, GuestViewModel>();
            containerRegistry.RegisterForNavigation<PersonCenterView, PersonCenterViewModel>();
        }
    }
}